package com.tencent.iov.cowin.befsale.clue.controller.cus.assembler;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.BaseAssembler;
import com.software.boot.common.model.rest.BaseRequest;
import com.software.boot.common.model.rest.BaseResponse;
import com.software.boot.common.util.DateUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.SCRMRequestHeaderEnum;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.request.UpdateClueIntentionRequest;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.response.ClueFailResponse;
import com.tencent.iov.cowin.befsale.clue.controller.clue.vo.response.ClueOrderResponse;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.AddCustomerFollowRecordRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateClueEmpRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.request.UpdateCustomerFollowRecordRequest;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerFollowRecordResponse;
import com.tencent.iov.cowin.befsale.clue.controller.cus.vo.response.CustomerResponse;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.Clue;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueFail;
import com.tencent.iov.cowin.befsale.clue.domain.clue.po.ClueIntentionVehicle;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.DealerCustomerFollowRecord;
import com.tencent.iov.cowin.befsale.clue.value.enums.*;
import com.tencent.iov.cowin.befsale.clue.value.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: xiamu
 * @Date: 2021-12-21 17:29:58
 * @Description: 经销商线索跟进记录适配器，负责vo<->entity、vo<->po的转换和必要的参数检查，行为
 */
@Slf4j
public class DealerCustomerFollowRecordAssembler extends BaseAssembler<BaseResponse, BaseRequest, DealerCustomerFollowRecord> {
    public static AddCustomerFollowRecordRequest initAddDealerCustomerFollowRecordRequest(AddCustomerFollowRecordRequest request) {
        if (EmptyUtils.isEmpty(request.getCorpId())) {
            request.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getTenantId())) {
            request.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedBy())) {
            request.setCreatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedBy())) {
            request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedName())) {
            request.setCreatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getUpdatedName())) {
            request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getDealerId())) {
            request.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
        }
        if (EmptyUtils.isEmpty(request.getCreatedTime())) {
            request.setCreatedTime(new Date());
        }
        if (EmptyUtils.isEmpty(request.getUpdatedTime())) {
            request.setUpdatedTime(new Date());
        }
        request.setDeleted(DeletedEnum.EXIST.getCode());
        return request;
    }

    public static UpdateCustomerFollowRecordRequest initUpdateDealerCustomerFollowRecordRequest(UpdateCustomerFollowRecordRequest request) {
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    public static DealerCustomerFollowRecord initUpdateDealerCustomerFollowRecord(DealerCustomerFollowRecord request) {
        request.setUpdatedBy(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode()));
        request.setUpdatedName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return request;
    }

    public static AddCustomerFollowRecordRequest initAddByDistribute(Clue clue, UpdateClueEmpRequest request, CustomerResponse customerResponse) {
        AddCustomerFollowRecordRequest addFollowRecordRequest = new AddCustomerFollowRecordRequest();
        addFollowRecordRequest.setClueId(clue.getId());
        addFollowRecordRequest.setCusId(clue.getCusId());

        Date operateTime = EmptyUtils.isEmpty(addFollowRecordRequest.getOperateTime()) ? new Date() : addFollowRecordRequest.getOperateTime();
        addFollowRecordRequest.setOperateTime(operateTime);

        addFollowRecordRequest.setExpireStatus(ExpireStatusEnum.EXPIRY_IN.getCode());
        addFollowRecordRequest.setExpireTime(0L);
        addFollowRecordRequest.setFollowEmpId(request.getFollowEmpId());
        addFollowRecordRequest.setFollowEmpName(request.getFollowEmpName());
        addFollowRecordRequest.setStatus(FollowRecordStatusEnum.DISTRIBUTION.getCode());
        addFollowRecordRequest.setSecondStatus(FollowRecordSecondStatusEnum.DISTRIBUTION.getCode());
        if (EmptyUtils.isNotEmpty(customerResponse) && EmptyUtils.isNotEmpty(customerResponse.getFollowEmpId())) {
            addFollowRecordRequest.setBefFollowEmpId(customerResponse.getFollowEmpId());
            addFollowRecordRequest.setBefFollowEmpName(customerResponse.getFollowEmpName());
            addFollowRecordRequest.setSecondStatus(FollowRecordSecondStatusEnum.CHANGE_DISTRIBUTION.getCode());
        }
        return initAddDealerCustomerFollowRecordRequest(addFollowRecordRequest);
    }

    public static AddCustomerFollowRecordRequest initAddByFailClue(Clue clue, UpdateClueEmpRequest request, ClueFail clueFail, CustomerResponse customerResponse) {
        AddCustomerFollowRecordRequest addFollowRecordRequest = new AddCustomerFollowRecordRequest();
        addFollowRecordRequest.setClueId(clue.getId());
        addFollowRecordRequest.setCusId(clue.getCusId());

        Date operateTime = EmptyUtils.isEmpty(addFollowRecordRequest.getOperateTime()) ? new Date() : addFollowRecordRequest.getOperateTime();
        addFollowRecordRequest.setOperateTime(operateTime);

        addFollowRecordRequest.setExpireStatus(ExpireStatusEnum.EXPIRY_IN.getCode());
        addFollowRecordRequest.setExpireTime(0L);
        addFollowRecordRequest.setFollowEmpId(request.getFollowEmpId());
        addFollowRecordRequest.setFollowEmpName(request.getFollowEmpName());
        if (EmptyUtils.isNotEmpty(customerResponse) && EmptyUtils.isNotEmpty(customerResponse.getFollowEmpId())) {
            addFollowRecordRequest.setBefFollowEmpId(customerResponse.getFollowEmpId());
            addFollowRecordRequest.setBefFollowEmpName(customerResponse.getFollowEmpName());
        }

        //战败信息
        FailStatusEnum failStatusEnum = FailStatusEnum.getByCode(request.getFailStatus());
        if (EmptyUtils.isEmpty(failStatusEnum)) {
            throw new BusinessException(NEError.BUSINESS_ERROR, "战败枚举不存在");
        }

        addFollowRecordRequest.setFailId(clueFail.getId());
        addFollowRecordRequest.setRemark(request.getApproveRemark());
        switch (failStatusEnum) {
            case AGREE:
                String followEmpId = SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode());
                if (EmptyUtils.isNotEmpty(followEmpId)) {
                    addFollowRecordRequest.setFollowEmpId(Long.parseLong(followEmpId));
                }
                addFollowRecordRequest.setFollowEmpName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
                addFollowRecordRequest.setStatus(FollowRecordStatusEnum.APPROVAL.getCode());
                addFollowRecordRequest.setSecondStatus(FollowRecordSecondStatusEnum.APPROVAL_FAIL_AGREE.getCode());
                break;
            case ACTIVATE:
                addFollowRecordRequest.setStatus(FollowRecordStatusEnum.APPROVAL.getCode());
                addFollowRecordRequest.setSecondStatus(FollowRecordSecondStatusEnum.APPROVAL_FAIL_ACTIVATE.getCode());
                addFollowRecordRequest.setNextFollowPlanId(request.getPlanFollowId());
                break;
            case REFUSE:
                addFollowRecordRequest.setStatus(FollowRecordStatusEnum.APPROVAL.getCode());
                addFollowRecordRequest.setSecondStatus(FollowRecordSecondStatusEnum.APPROVAL_FAIL_REFUSED.getCode());
            default:
                break;
        }
        return initAddDealerCustomerFollowRecordRequest(addFollowRecordRequest);
    }

    public static AddCustomerFollowRecordRequest initAddByIntentionChange(String clueIntentionStr, Clue clue, UpdateClueIntentionRequest updateClueIntentionRequest, List<ClueIntentionVehicle> clueIntentionVehicles) {
        AddCustomerFollowRecordRequest request = new AddCustomerFollowRecordRequest();
        request.setClueId(updateClueIntentionRequest.getClueId());
        request.setCusId(updateClueIntentionRequest.getCusId());
        request.setStatus(FollowRecordStatusEnum.CHANGE_INTENTION.getCode());
        request.setSecondStatus(FollowRecordSecondStatusEnum.CHANGE_INTENTION.getCode());
        String clueGrade = EmptyUtils.isEmpty(updateClueIntentionRequest.getIntentionGrade()) ? clue.getIntentionGrade() : updateClueIntentionRequest.getIntentionGrade();
        request.setIntentionGrade(clueGrade);
        request.setVehInfo(JSON.toJSONString(clueIntentionVehicles));
        request.setIntentionTags(clueIntentionStr);
        String followEmpId = SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode());
        if (EmptyUtils.isNotEmpty(followEmpId)) {
            request.setFollowEmpId(Long.parseLong(followEmpId));
        }
        request.setFollowEmpName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        return initAddDealerCustomerFollowRecordRequest(request);
    }

    /**
     * 转化跟进状态
     *
     * @param customerFollowRecord 跟进记录信息
     */
    public static void fillCustomerFollowRecordStatus(AddCustomerFollowRecordRequest customerFollowRecord) {
        String followEmpId = SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_ID.getCode());
        if (EmptyUtils.isNotEmpty(followEmpId)) {
            customerFollowRecord.setFollowEmpId(Long.parseLong(followEmpId));
        }

        if (EmptyUtils.isEmpty(customerFollowRecord.getFollowContent())) {
            return;
        }

        FollowContentEnum followContentEnum = FollowContentEnum.getByCode(Integer.parseInt(customerFollowRecord.getFollowContent()));
        if (EmptyUtils.isEmpty(followContentEnum)) {
            return;
        }

        customerFollowRecord.setFollowEmpName(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_EMP_NAME.getCode()));
        switch (followContentEnum) {
            case PRODUCT_INTRO:
                customerFollowRecord.setStatus(FollowRecordStatusEnum.FOLLOW_UP.getCode());
                customerFollowRecord.setSecondStatus(FollowRecordSecondStatusEnum.FOLLOW_PRODUCT_INTRODUCE.getCode());
                break;
            case CHECKIN:
                customerFollowRecord.setStatus(FollowRecordStatusEnum.FOLLOW_UP.getCode());
                customerFollowRecord.setSecondStatus(FollowRecordSecondStatusEnum.FOLLOW_CHECKIN.getCode());
                break;
            case DRIVE_TEST:
                customerFollowRecord.setStatus(FollowRecordStatusEnum.FOLLOW_UP.getCode());
                customerFollowRecord.setSecondStatus(FollowRecordSecondStatusEnum.FOLLOW_DRIVE_TEST.getCode());
                break;
            case ORDER:
                customerFollowRecord.setStatus(FollowRecordStatusEnum.FOLLOW_UP.getCode());
                customerFollowRecord.setSecondStatus(FollowRecordSecondStatusEnum.FOLLOW_ORDERED.getCode());
                break;
            case DELIVERY:
                customerFollowRecord.setStatus(FollowRecordStatusEnum.FOLLOW_UP.getCode());
                customerFollowRecord.setSecondStatus(FollowRecordSecondStatusEnum.FOLLOW_DELIVERY.getCode());
                break;
            default:
                break;
        }
    }

    public static void fillFollowDetailStr(CustomerFollowRecordResponse record, Map<String, String> dicMapByCode) {
        if (EmptyUtils.isEmpty(record.getSecondStatus())) {
            log.error("线索分类2不可以为null");
            return;
        }
        FollowRecordSecondStatusEnum secondStatusEnum = FollowRecordSecondStatusEnum.getByCode(record.getSecondStatus());
        if (EmptyUtils.isEmpty(secondStatusEnum)) {
            log.error("线索分类2不可以为null");
            return;
        }


        List<String> followContentList = Lists.newArrayList();
        switch (secondStatusEnum) {
            case FOLLOW_PRODUCT_INTRODUCE:
                fillFollowProduct(record, dicMapByCode, followContentList);
                break;
            case FOLLOW_CHECKIN:
                fillFollowCheckIn(record, followContentList);
                break;
            case FOLLOW_DRIVE_TEST:
                fillFollowDriveTest(record, followContentList);
                break;
            case FOLLOW_ORDERED:
                fillFollowOrdered(record, followContentList);
                break;
            case FOLLOW_DELIVERY:
                if (EmptyUtils.isNotEmpty(record.getDeliveryName())) {
                    followContentList.add(String.format("%s:%s", "交车姓名", record.getDeliveryName()));
                }
                if (EmptyUtils.isNotEmpty(record.getDeliveryPhone())) {
                    followContentList.add(String.format("%s:%s", "交车手机", record.getDeliveryPhone()));
                }
                break;
            case FOLLOW_CONSUMED:
                if (EmptyUtils.isNotEmpty(record.getClueOrder())) {
                    ClueOrderResponse clueOrder = record.getClueOrder();
                    //todo 添加订单号
                }
                break;
            case FOLLOW_FAIL_APPLY:
                fillFollowFailApply(record, dicMapByCode, followContentList);
                break;
            case FOLLOW_DRIVE_APPOINT:
                fillFollowDriveAppoint(record, followContentList);
                break;
            default:
                break;
        }

        if (EmptyUtils.isEmpty(followContentList)) {
            return ;
        }
        record.setFollowDetail(StringUtils.join(followContentList, " | "));
    }

    private static void fillFollowDriveAppoint(CustomerFollowRecordResponse record, List<String> followContentList) {
        if (EmptyUtils.isNotEmpty(record.getOperateTime())) {
            followContentList.add(String.format("%s:%s", "试驾预约时间", DateUtil.toDateString(record.getOperateTime())));
        }
        if (EmptyUtils.isNotEmpty(record.getVehInfo())) {
            ClueIntentionVehicle vehicle = JSON.parseObject(record.getVehInfo(), ClueIntentionVehicle.class);
            if (EmptyUtils.isNotEmpty(vehicle) && EmptyUtils.isNotEmpty(vehicle.getSeriesName())) {
                followContentList.add(String.format("%s:%s", "试驾车系", vehicle.getSeriesName()));
            }
            if (EmptyUtils.isNotEmpty(vehicle) && EmptyUtils.isNotEmpty(vehicle.getModelName())) {
                followContentList.add(String.format("%s:%s", "试驾车型", vehicle.getModelName()));
            }
        }
    }

    private static void fillFollowDriveTest(CustomerFollowRecordResponse record, List<String> followContentList) {
        if (EmptyUtils.isNotEmpty(record.getOperateTime())) {
            followContentList.add(String.format("%s:%s", "试乘试驾时间", DateUtil.toDateString(record.getOperateTime())));
        }
        if (EmptyUtils.isNotEmpty(record.getVehInfo())) {
            ClueIntentionVehicle vehicle = JSON.parseObject(record.getVehInfo(), ClueIntentionVehicle.class);
            if (EmptyUtils.isNotEmpty(vehicle) && EmptyUtils.isNotEmpty(vehicle.getSeriesName())) {
                followContentList.add(String.format("%s:%s", "车系", vehicle.getSeriesName()));
            }
            if (EmptyUtils.isNotEmpty(vehicle) && EmptyUtils.isNotEmpty(vehicle.getModelName())) {
                followContentList.add(String.format("%s:%s", "车型", vehicle.getModelName()));
            }
        }
    }

    private static void fillFollowCheckIn(CustomerFollowRecordResponse record, List<String> followContentList) {
        if (EmptyUtils.isNotEmpty(record.getCheckinTime())) {
            followContentList.add(String.format("%s:%s", "到店日期", DateUtil.toDateString(record.getCheckinTime())));
        }
        if (EmptyUtils.isNotEmpty(record.getCheckoutTime())) {
            followContentList.add(String.format("%s:%s", "离店日期", DateUtil.toDateString(record.getCheckoutTime())));
        }
        if (EmptyUtils.isNotEmpty(record.getFollowRemark())) {
            followContentList.add(String.format("%s:%s", "跟进备注", record.getFollowRemark()));
        }
    }

    private static void fillFollowFailApply(CustomerFollowRecordResponse record, Map<String, String> dicMapByCode, List<String> followContentList) {
        if (EmptyUtils.isNotEmpty(record.getClueFail())) {
            ClueFailResponse clueFail = record.getClueFail();
            List<String> failReasonNames = CommonUtil.getDicNameByObject(clueFail.getFailReasonCodes(), dicMapByCode);
            if (EmptyUtils.isNotEmpty(failReasonNames)) {
                String prodName = StringUtils.join(failReasonNames, ";");
                followContentList.add(String.format("%s:%s", "战败主因", prodName));
            }
            if (EmptyUtils.isNotEmpty(clueFail.getFailRemark())) {
                followContentList.add(String.format("%s:%s", "战败备注", clueFail.getFailRemark()));
            }
        }
    }

    private static void fillFollowOrdered(CustomerFollowRecordResponse record, List<String> followContentList) {
        if (EmptyUtils.isNotEmpty(record.getOperateTime())) {
            followContentList.add(String.format("%s:%s", "下订时间", DateUtil.toDateString(record.getOperateTime())));
        }
        if (EmptyUtils.isNotEmpty(record.getClueOrder())) {
            ClueOrderResponse clueOrder = record.getClueOrder();
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderSeriesName())) {
                followContentList.add(String.format("%s:%s", "车系", clueOrder.getOrderSeriesName()));
            }
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderModelName())) {
                followContentList.add(String.format("%s:%s", "车型", clueOrder.getOrderModelName()));
            }
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderMoney())) {
                followContentList.add(String.format("%s:%s", "定金", clueOrder.getOrderMoney()));
            }
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderName())) {
                followContentList.add(String.format("%s:%s", "下定姓名", clueOrder.getOrderName()));
            }
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderPhone())) {
                followContentList.add(String.format("%s:%s", "下定手机", clueOrder.getOrderPhone()));
            }
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderDeliveryTime())) {
                followContentList.add(String.format("%s:%s", "交车日期:", DateUtil.toDateString(clueOrder.getOrderDeliveryTime())));
            }
            if (EmptyUtils.isNotEmpty(clueOrder.getOrderAddress())) {
                followContentList.add(String.format("%s:%s", "地址", clueOrder.getOrderAddress()));
            }
        }
    }

    private static void fillFollowProduct(CustomerFollowRecordResponse record, Map<String, String> dicMapByCode, List<String> followContentList) {
        List<String> prodNames = CommonUtil.getDicNameByObject(record.getProdCode(), dicMapByCode);
        if (EmptyUtils.isNotEmpty(prodNames)) {
            String prodName = StringUtils.join(prodNames, ";");
            followContentList.add(String.format("%s:%s", "产品介绍", prodName));
        }
        if (EmptyUtils.isNotEmpty(record.getFollowRemark())) {
            followContentList.add(String.format("%s:%s", "跟进备注", record.getFollowRemark()));
        }
    }
}
